﻿using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;

namespace AutoClub.Support
{
    /// <summary>
    /// Builder para la creacion de spans html, con la propiedad de agregar inner spans.
    /// </summary>
    public class SpanBuilder
    {
        #region Fields

        private TagBuilder builder;
        private IList<ClassContentForSpan> nestedSpans;

        #endregion

        #region Methods

        /// <summary>
        /// Construye una instancia de span builder con su main css class y texto dentro de este
        /// </summary>
        public SpanBuilder(string cssClass)
        {
            this.builder = new TagBuilder("span");
            this.nestedSpans = new List<ClassContentForSpan>();

            this.builder.AddCssClass(cssClass);
        }

        public void AddNestedSpan(string cssClass, string innerHtml)
        {
            this.nestedSpans.Add(new ClassContentForSpan(cssClass, innerHtml));
        }


        public override string ToString()
        {
            StringBuilder innerSpans = new StringBuilder();
            foreach (var item in this.nestedSpans)
            {
                TagBuilder actual = new TagBuilder("span");
                actual.AddCssClass(item.CssClass);
                actual.InnerHtml = item.InnerHtml;
                innerSpans.Append(actual.ToString(TagRenderMode.Normal));
            }
            if (innerSpans.Length > 0)
                this.builder.InnerHtml = innerSpans.ToString();

            return this.builder.ToString(TagRenderMode.Normal);
        }

        #endregion
    }

    internal class ClassContentForSpan
    {
        #region Fields

        public string CssClass { get; private set; }
        public string InnerHtml { get; private set; }

        #endregion

        public ClassContentForSpan(string cssClass, string innerHtml)
        {
            this.CssClass = cssClass;
            this.InnerHtml = innerHtml;
        }
    }
}